using System;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace FSS.Core.Util
{
    public class Util
    {
        /// <summary>
        /// convert obj to long
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long Obj2Long(object obj)
        {
            try
            {

                if (obj == null || "".Equals(obj)) return 0;
                decimal d = Convert.ToDecimal(obj.ToString().Replace(",", ""));

                // return long.Parse(obj.ToString().Replace(",", ""));

                return Convert.ToInt64(d);
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// convert obj to int
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Obj2Int(object obj)
        {
            try
            {
                if (obj == null || "".Equals(obj)) return 0;
                decimal d = Convert.ToDecimal(obj.ToString().Replace(",", ""));
                return Convert.ToInt32(d);
            }
            catch { return 0; }
        }
        /// <summary>
        /// convert obj to Double
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Double Obj2Double(object obj)
        {
            try
            {
                if (obj == null || "".Equals(obj)) return 0;
                decimal d = Convert.ToDecimal(obj.ToString().Replace(",", ""));
                return Convert.ToDouble(d);
            }
            catch { return 0; }
        }
        public static decimal Obj2Decimal(object obj)
        {
            try
            {
                if (obj == null || "".Equals(obj)) return 0;
                decimal d = Convert.ToDecimal(obj.ToString().Replace(",", ""));
                return d;
            }
            catch { return 0; }
        }
        /// <summary>
        /// Check is blank or not
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Boolean IsBlank(object obj)
        {
            try
            {
                if (obj == null || "".Equals(obj.ToString().Trim())) return true;
                return false;
            }
            catch { return true; }
        }
        /// <summary>
        /// Convert String to DateTime
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static DateTime Str2DataTime(String Str)
        {
            try
            {
                if (IsBlank(Str)) return DateTime.Now;
                return DateTime.Parse(Str.Trim());
            }
            catch { return DateTime.Now; }
        }

        /// <summary>
        /// Convert String to date string base on pattern
        /// ex: ConvertToDateString("09/03/1980","MM/dd/yyyy");//--> 03/09/1980 
        /// </summary>
        /// <param name="aDate"></param>
        /// <param name="aPattern"></param>
        /// <returns></returns>
        public static String ConvertToDateString(String aDate, String aPattern)
        {
            DateTime dDate;
            if (aDate == null || aDate.Equals(""))
            {
                return "";
            }
            try
            {
                System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-GB");
                dDate = Convert.ToDateTime(aDate, culture);
            }
            catch
            {
                return "";
            }
            return dDate.ToString(aPattern, null);
        }
        /// <summary>
        /// convert String to Date
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime ConvertToDate(String str)
        {
            DateTime dDate;
            if (IsBlank(str))
            {
                return DateTime.Today;
            }
            try
            {
                System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-GB");
                dDate = Convert.ToDateTime(str, culture);
            }
            catch
            {
                return DateTime.Today;
            }
            return dDate;
        }

        public static string GetString(string sText)
        {
            string pw = sText;
            int convertedPw;
            string sString = "";
            int counter = 0;
            int uLetters = 0;
            int lLetters = 0;
            int numbers = 0;
            int symbols = 0;

            CharEnumerator charEnum = pw.GetEnumerator();
            while (charEnum.MoveNext())
            {
                convertedPw = Convert.ToInt32(pw[counter]);

                if ((convertedPw >= 65) && (convertedPw <= 90))
                {
                    sString += pw[counter];
                    uLetters++;
                }

                if ((convertedPw >= 97) && (convertedPw <= 122))
                {
                    sString += pw[counter];
                    lLetters++;
                }

                if ((convertedPw >= 48) && (convertedPw <= 57))
                {
                    numbers++;
                }

                if ((convertedPw >= 33) && (convertedPw <= 47))
                {
                    symbols++;
                }

                if ((convertedPw >= 58) && (convertedPw <= 64))
                {
                    symbols++;
                }

                if ((convertedPw >= 91) && (convertedPw <= 96))
                {
                    symbols++;
                }

                if ((convertedPw >= 123) && (convertedPw <= 126))
                {
                    symbols++;
                }

                counter++;
            }
            return sString;
        }

        public static string GetNumbers(string sText)
        {
            string pw = sText;
            int convertedPw;
            string sNumber = "";
            int counter = 0;
            int uLetters = 0;
            int lLetters = 0;
            int numbers = 0;
            int symbols = 0;

            CharEnumerator charEnum = pw.GetEnumerator();
            while (charEnum.MoveNext())
            {
                convertedPw = Convert.ToInt32(pw[counter]);

                if ((convertedPw >= 65) && (convertedPw <= 90))
                {
                    uLetters++;
                }

                if ((convertedPw >= 97) && (convertedPw <= 122))
                {
                    lLetters++;
                }

                if ((convertedPw >= 48) && (convertedPw <= 57))
                {
                    sNumber += pw[counter];
                    numbers++;
                }

                if ((convertedPw >= 33) && (convertedPw <= 47))
                {
                    symbols++;
                }

                if ((convertedPw >= 58) && (convertedPw <= 64))
                {
                    symbols++;
                }

                if ((convertedPw >= 91) && (convertedPw <= 96))
                {
                    symbols++;
                }

                if ((convertedPw >= 123) && (convertedPw <= 126))
                {
                    symbols++;
                }

                counter++;
            }
            return sNumber;
        }

        /// <summary>
        /// Correct sql strings
        /// </summary>
        /// <param name="Field"></param>
        /// <returns></returns>
        public static String SQLStringFormat(String Field)
        {
            try
            {
                if (IsBlank(Field)) return "";
                return Field.Replace("'", "''");
            }
            catch
            {
                return "";
            }
        }
        public static String GetTodayString()
        {
            String day = DateTime.Today.Day.ToString();
            day = day.PadLeft(2, '0');
            String month = DateTime.Today.Month.ToString();
            month = month.PadLeft(2, '0');
            String year = DateTime.Today.Year.ToString();
            return day + month + year;
        }

        public static String GetHoutMinuteString()
        {
            String Hour = DateTime.Now.Hour.ToString();
            Hour = Hour.PadLeft(2, '0');
            String Minute = DateTime.Now.Minute.ToString();
            Minute = Minute.PadLeft(2, '0');
            return Hour + ":" + Minute;
        }


        /// <summary>
        /// Copies the object instance of a particular type to the destination type.
        /// </summary>
        /// <param name="source">The Type of the source object instance.</param>
        /// <param name="sourceToCopy">The actual instance to be coverted to destination type.</param>
        /// <param name="destination">The desination Type to which the source object instance needs
        /// to be comverted.</param>
        /// <returns>An object instance of the destination type.</returns>
        public static object Copy(Type source, object sourceToCopy, Type destination)
        {
            XmlSerializer serializer = new XmlSerializer(source);
            MemoryStream memoryStream = new MemoryStream();

            XmlSerializerNamespaces nameSpacesList = new XmlSerializerNamespaces();
            nameSpacesList.Add("", "");

            serializer.Serialize(memoryStream, sourceToCopy, nameSpacesList);

            byte[] byteArr = memoryStream.ToArray();
            ASCIIEncoding encoder = new ASCIIEncoding();
            string xmlString = encoder.GetString(byteArr);

            ASCIIEncoding newEncoder = new ASCIIEncoding();
            byte[] newByteArr = newEncoder.GetBytes(xmlString);
            MemoryStream newMemoryStream = new MemoryStream(newByteArr);

            XmlSerializer deSer = new XmlSerializer(destination);

            object deserializedObject = deSer.Deserialize(newMemoryStream);

            return deserializedObject;

        }

        public static String SafeTrim(Object str)
        {
            if (IsBlank(str)) return "";
            return str.ToString().Trim();

        }
        /// <summary>
        /// Get the first day of the month for
        /// any full date submitted
        /// </summary>
        /// <param name="dtDate"></param>
        /// <returns></returns>
        public static DateTime GetFirstDayOfMonth(DateTime dtDate)
        {
            // set return value to the first day of the month
            // for any date passed in to the method
            // create a datetime variable set to the passed in date
            DateTime dtFrom = dtDate;
            // remove all of the days in the month
            // except the first day and set the
            // variable to hold that date
            dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));
            // return the first day of the month
            return dtFrom;
        }

        /// <summary>
        /// Get the first day of the month for a
        /// month passed by it's integer value
        /// </summary>
        /// <param name="iMonth"></param>
        /// <returns></returns>
        public static DateTime GetFirstDayOfMonth(int iMonth)
        {
            // set return value to the last day of the month
            // for any date passed in to the method
            // create a datetime variable set to the passed in date
            DateTime dtFrom = new DateTime(DateTime.Now.Year, iMonth, 1);
            // remove all of the days in the month
            // except the first day and set the
            // variable to hold that date
            dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));
            // return the first day of the month
            return dtFrom;
        }
        /// <summary>
        /// Get the last day of the month for any
        /// full date
        /// </summary>
        /// <param name="dtDate"></param>
        /// <returns></returns>
        public static DateTime GetLastDayOfMonth(DateTime dtDate)
        {
            // set return value to the last day of the month
            // for any date passed in to the method
            // create a datetime variable set to the passed in date
            DateTime dtTo = dtDate;
            // overshoot the date by a month
            dtTo = dtTo.AddMonths(1);
            // remove all of the days in the next month
            // to get bumped down to the last day of the
            // previous month
            dtTo = dtTo.AddDays(-(dtTo.Day));
            // return the last day of the month
            return dtTo;
        }
        /// <summary>
        /// Get the last day of a month expressed by it's
        /// integer value
        /// </summary>
        /// <param name="iMonth"></param>
        /// <returns></returns>
        public static DateTime GetLastDayOfMonth(int iMonth)
        {
            // set return value to the last day of the month
            // for any date passed in to the method
            // create a datetime variable set to the passed in date
            DateTime dtTo = new DateTime(DateTime.Now.Year, iMonth, 1);
            // overshoot the date by a month
            dtTo = dtTo.AddMonths(1);
            // remove all of the days in the next month
            // to get bumped down to the last day of the
            // previous month
            dtTo = dtTo.AddDays(-(dtTo.Day));
            // return the last day of the month
            return dtTo;
        }

    }
    public class Format
    {
        /// <summary>
        /// Convert to Int
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static int ConvertToInt(object Value)
        {
            try
            {
                return Convert.ToInt32(Value);
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public static Int64 ConvertToLong(object Value)
        {
            try
            {
                return Convert.ToInt64(Value);
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
